
var { Point3D } = require("./Point3d");
const { Plot } = require("../Plot3d");
/**
 * 
 * @param {Point3D[]} list 
 */
function step1(list) {
  var distance = [];
  for (let i = 0; i < list.length; i++) {
    list[i].ax = 0;
    list[i].ay = 0;
    list[i].az = 0;
    for (let j = 0; j < list.length; j++) {
      // const element = array[index];
      if (i == j) {
        continue
      }
      let p = list[i];

      let pv = { dvx: p.x - list[j].x, dvy: p.y - list[j].y, dvz: p.z - list[j].y };
      distance[j] = Point3D.len(list[i], list[j])
      if (distance[j] < 10) {
        val = -1
      } else {
        var val = 1
      }

      list[i].ax += pv.dvx / (distance[j] * distance[j]) * val
      list[i].ay += pv.dvy / (distance[j] * distance[j]) * val;
      list[i].az += pv.dvz / (distance[j] * distance[j]) * val;
      // console.log("v::",[j],list[j].vx, list[j].vy, list[j].vz);
    }
    list[i].vx += list[i].ax;
    list[i].vy += list[i].ay;
    list[i].vz += list[i].az;
    // console.log("xyz::",list[i].x, list[i].y, list[i].z);
    list[i].x += list[i].vx
    list[i].y += list[i].vy
    list[i].z += list[i].vz
    // const element = array[index];
    // debug
  }
}

function randInit3D(len) {
  var list = [];
  for (let index = 0; index < len; index++) {
    // const element = array[index];
    var obj = {
      x: (Math.random() - 0.5) * 10,
      y: (Math.random() - 0.5) * 10,
      z: (Math.random() - 0.5) * 10,
      vx: 0,
      vy: 0,
      vz: 0,
      ax: 0,
      ay: 0,
      az: 0,
    }
    list.push(obj)
  }
  return list;
}
var t3 = randInit3D(1000)
var plot = new Plot(document.querySelector("#myCanvas"));

var t = setInterval(() => {
  step1(t3)
  plot.draw3D(t3)
}, 10);

